package com.dkh.recommend.controller;


import cn.hutool.core.util.StrUtil;
import com.dkh.recommend.dto.LoginFormDTO;
import com.dkh.recommend.dto.PasswordDTO;
import com.dkh.recommend.dto.Result;
import com.dkh.recommend.dto.UserDTO;
import com.dkh.recommend.entity.TbUser;
import com.dkh.recommend.service.ITbUserService;
import com.dkh.recommend.utils.UserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dkh
 * @since 2022-12-14
 */
@Slf4j
@RestController
@Api(value = "用户", tags = "用户-相关", description="用户-相关")
@RequestMapping("/user")
public class TbUserController {

    @Autowired
    private ITbUserService tbUserService;

//    登录功能
    @ApiOperation("用户登录")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "loginform", value = "账号密码", required = true, dataType = "LoginFormDTO", paramType = "body")})
    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginform){
//        实现登录功能
    return tbUserService.login(loginform);

    }

//    注册功能
@ApiOperation("用户注册")
@ApiImplicitParams({
        @ApiImplicitParam(name = "tbUser", value = "用户信息", required = true, dataType = "TbUser", paramType = "body")})
@PostMapping("/register")
    public Result register(@RequestBody TbUser tbUser){
    //        实现注册功能
        return tbUserService.register(tbUser);

    }

    @ApiOperation("/me")
    @GetMapping("/me")
    public Result me(){
        // 获取当前登录的用户并返回
        UserDTO user = UserHolder.getUser();
        return Result.ok(user);
    }

    @ApiOperation("根据id获取用户信息")
    @GetMapping("/info/{userId}")
    public Result getUserById(@PathVariable String userId){
        return tbUserService.getUserById(userId);
    }

//    @GetMapping("/getbyid")
//    public Result GetById(@PathVariable("userId") Long userId){
//    return null;
//    }

    @ApiOperation("获取我的所有信息")
    @GetMapping("/myallinfo")
    public Result getMyAllInfoById2(){
        return tbUserService.getMyAllInfoById2();
    }



//    修改用户信息
@ApiOperation("用户修改")
@ApiImplicitParams({
        @ApiImplicitParam(name = "tbUser", value = "用户信息", required = true, dataType = "TbUser", paramType = "body")})
@PutMapping("/update")

    public Result updateUser(@RequestBody TbUser tbUser){
        //        修改用户信息
        return tbUserService.updateUser(tbUser);

    }

//    修改密码
    @PutMapping("/updatepws")
    public Result UpdatePassword(@RequestBody PasswordDTO passwordDTO){

        return tbUserService.UpdatePassword(passwordDTO);
    }



    //    退出登录
    @DeleteMapping("/outlogin")
    public Result OutLogin(@RequestBody String token){
        return tbUserService.OutLogin(token);
    }



}
